﻿Imports System.Reflection

''' <summary>
''' 更新ダーティオブジェクト
''' </summary>
''' <remarks>
''' 更新されたオブジェクトを示すクラス
''' </remarks>
Public Class UpdateDirtyObject
    Inherits DirtyObject

    ''' <summary>
    ''' コンストラクタ
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <param name="dirties">ダーティプロパティアーカイブ</param>
    ''' <remarks></remarks>
    Public Sub New(arch As ItemEntityArchive, instance As Object, dirties As ICollection(Of LiteralPropertyArchive))
        If arch Is Nothing Then Throw New ArgumentNullException("arch")
        If instance Is Nothing Then Throw New ArgumentNullException("instance")
        If dirties Is Nothing Then Throw New ArgumentNullException("dirties")

        Me.Archive = arch

        Me.Key = arch.Key
        Me.Instance = instance
        Me.InstanceType = Me.Archive.ClassType

        For Each item In dirties
            Me.DirtyPropertyArchives.Add(item)
        Next

        'SystemValueAttributeを強制追加する
        For Each item In (From el In Me.Archive.ItemArchives Where el.Value.HasSystemValue AndAlso el.Value.SystemValueAttribute.IsUpdateTrigger Select el.Value)
            Me.DirtyPropertyArchives.Add(item)
        Next
    End Sub

    Private _archive As ItemEntityArchive
    ''' <summary>
    ''' アイテムアーカイブ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Archive As ItemEntityArchive
        Get
            Return _archive
        End Get
        Private Set(ByVal value As ItemEntityArchive)
            _archive = value
        End Set
    End Property

    Private _instance As Object
    ''' <summary>
    ''' 現在のインスタンス
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Instance As Object
        Get
            Return _instance
        End Get
        Private Set(ByVal value As Object)
            _instance = value
        End Set
    End Property

    ''' <summary>
    ''' ダーティタイプ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property DirtyType As DirtyTypes
        Get
            Return DirtyTypes.Update
        End Get
    End Property

    Private _propertyArchives As New List(Of LiteralPropertyArchive)
    ''' <summary>
    ''' ダーティ値アーカイブ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property DirtyPropertyArchives() As ICollection(Of LiteralPropertyArchive)
        Get
            Return _propertyArchives
        End Get
    End Property

    ''' <summary>
    ''' ダーティプロパティ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property DirtyProperties As IEnumerable(Of LiteralProperty)
        Get
            Return (From el In Me.DirtyPropertyArchives Select el.PropertyInfo)
        End Get
    End Property

    ''' <summary>
    ''' リテラル名、アーカイブ値の辞書
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property KeyDictionay As Dictionary(Of String, LiteralPropertyArchive)
        Get
            Dim dic As New Dictionary(Of String, LiteralPropertyArchive)

            For Each item In Me.KeyProperties
                Dim name = item.LiteralName
                Dim val = Me.Archive.ItemArchives(name)
                dic.Add(name, val)
            Next

            Return dic
        End Get
    End Property
End Class
